Repository navigation
Trim the AC-3 decoder delay in transport streams - #21
Merged
Merged
Conversation
AC-3 and E-AC-3 decode 256 samples late. MP4 and MOV record that in their edit list, which FFmpeg applies, but MPEG-TS (AVCHD) records it nowhere, so on Windows and Linux AVCHD audio sat 5.3 ms earlier than on macOS, where AVFoundation trims the delay. The FFmpeg tier now starts those streams 256 samples later for whole-file, window and native decoding. On camera-original Sony HDR-CX6 and AVCHD Lite clips the portable engine now places the audio exactly where AVFoundation does.
Trimming 256 samples from the start of AC-3 in MPEG-TS leaves the last 256 samples of the container without audio. Stems and placement pads now fill that gap with silence, as they fill a camera's sub-frame tail, so an audio-only transport stream exports at its container length instead of failing with "Cannot decode audio for drift correction." Clean-media export through FFmpeg (cameras outside QuickTime and MP4) decodes the recorder with FFmpeg directly; it now skips the recorder's decoder delay too, instead of placing its audio 256 samples late.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AC-3 and E-AC-3 decode 256 samples (5.3 ms at 48 kHz) late. MP4 and MOV
record that in their edit list, which FFmpeg applies. MPEG-TS, the container
of AVCHD (
.MTS/.M2TS), records it nowhere, so FFmpeg's raw output keepsit, and on Windows and Linux AVCHD audio sat 5.3 ms earlier than on macOS,
where AVFoundation trims the delay.
The ffprobe parser now marks AC-3 and E-AC-3 streams in MPEG-TS with that
256-sample
decoder_delay, and the FFmpeg tier starts them that much laterfor whole-file analysis, refinement windows and native (export) decoding.
Every other stream keeps a delay of 0.
Exports follow the trimmed timing:
stems and placement pads fill that gap with silence, as they already fill
a camera's sub-frame tail, so an audio-only transport stream still exports
at its container length. Before, the shortfall failed the stem with
"Cannot decode audio for drift correction." Audio missing anywhere else is
still rejected.
recorder directly. It now skips the recorder's decoder delay too, instead
of placing its audio 256 samples late.
The fingerprint cache namespace is not bumped here: fingerprints of AC-3 in
MPEG-TS made before this change are 5.3 ms early. This ships after
fix/damaged-audio-packets, which moves the namespace toportable4; if itwere released without it,
portable3would need the bump.Evidence:
(The camera clips were synchronized against their own audio extracted from
the first video frame; what matters is that both engines now agree.)
Not changed: AAC in MPEG-TS. Its encoder priming is recorded nowhere either,
and the engines disagree in opposite directions (AVFoundation trims 2112
samples, FFmpeg none, an FFmpeg-encoded stream primes 1024), so there is no
reliable correction. AVCHD uses AC-3 or LPCM.
Tests, each failing without its fix:
ac3_in_transport_streams_starts_on_timeplaces a click encoded at 1.5 sexactly, from the head, after a seek, and in a refinement window. Without
the trim it lands at sample 72256 instead of 72000.
stems_of_audio_with_a_trimmed_decoder_delay_keep_their_length: a stem ofa whole audio-only transport stream has the container's length, with the
click on time.
ffmpeg_remux_trims_the_recorder_decoder_delay: a clean-media export of anMPEG-TS camera places the recorder's click at sample 31200, not 31456.
Validation:
cargo fmt --all -- --checkcargo clippy --locked --workspace --all-targets -- -D warningscargo test --locked --workspace: 376 passed, 1 ignored